package com.neu.ui.notifications;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.neu.R;
import com.neu.activity.LoginActivity;
import com.neu.bean.User;
import com.neu.ui.BaseFragment;
import com.neu.ui.dashboard.DashboardViewModel;
import com.neu.util.VolleyUtil;

public class NotificationsFragment extends BaseFragment {

    private Button btnPre;
    private Button btnGo;
    private ImageView imgAvatar;
    private TextView txtUser;
    private NotificationsViewModel model;

    @Override
    protected int layoutId() {
        return R.layout.fragment_notifications;
    }

    @Override
    protected void initView(View view) {
        btnPre = view.findViewById(R.id.btnPre);
        btnPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

        imgAvatar=view.findViewById(R.id.imgAvatar);
        txtUser=view.findViewById(R.id.txtUser);
        btnGo=view.findViewById(R.id.btnGo);
        //退出
        btnGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
                VolleyUtil.TOKEN=null;
                getActivity().finish();

            }
        });
    }

    @Override
    protected void initData(Context context) {
        model=new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(NotificationsViewModel.class);
        initUser();
    }

    private void initUser() {
        if(VolleyUtil.TOKEN!=null){
            model.getUser().observe(getViewLifecycleOwner(), new Observer<User>() {
                @Override
                public void onChanged(User user) {

                        //添加默认图片
                        RequestOptions requestOptions = new RequestOptions();
                        requestOptions.placeholder(R.drawable.ic_launcher_background);
                        requestOptions.circleCropTransform();

                        Glide.with(getContext()).load(VolleyUtil.BASE_URL+user.getUser().getAvatar()).apply(requestOptions).into(imgAvatar);
                        txtUser.setText(user.getUser().getNickName());

                        imgAvatar.setVisibility(View.VISIBLE);
                        txtUser.setVisibility(View.VISIBLE);
                        btnGo.setVisibility(View.VISIBLE);
                        btnPre.setVisibility(View.INVISIBLE);

                }
            });
        }else{
            imgAvatar.setVisibility(View.INVISIBLE);
            txtUser.setVisibility(View.INVISIBLE);
            btnGo.setVisibility(View.INVISIBLE);
            btnPre.setVisibility(View.VISIBLE);
        }
    }
}